import { createRouter, createWebHistory } from 'vue-router';
import { user } from '../store/user.js';

const routes = [
	{
		name: 'index',
		path: '/',
		component: () => import('../views/index.vue'),
	},
	{
		name: 'login',
		path: '/login',
		component: () => import('../views/login.vue'),
	},
	{
		name: 'aboutMe',
		path: '/aboutMe',
		component: () => import('../views/aboutMe.vue'),
	},
	{
		name: 'article',
		path: '/article',
		component: () => import('../views/article.vue'),
	},
	{
		name: 'board',
		path: '/board',
		component: () => import('../views/board.vue'),
	},
	{
		name: 'discover',
		path: '/discover',
		component: () => import('../views/discover.vue'),
	},
	{
		name: 'register',
		path: '/register',
		component: () => import('../views/register.vue'),
	},
	{
		name: 'addBlog',
		path: '/addBlog',
		component: () => import('../views/addBlog.vue'),
	},
	{
		name: 'blogPage',
		path: '/blogPage',
		component: () => import('../views/blogPage.vue'),
	},
	{
		name: 'blogSearchDetail',
		path: '/blogSearchDetail',
		component: () => import('../views/blogSearchDetail.vue'),
	},
	{
		name: 'manage',
		path: '/manage',
		beforeEnter: (to, from, next) => {
			const userStore = user();
			if (from.path !== '/') {
				ElMessage({
					title: 'Warning',
					message: '尚未通过验证',
					type: 'warning',
				});
				return next({ path: '/' });
			} else {
				if (userStore.$state.manageConfirm) {
					next();
				} else {
					ElMessage({
						title: 'Warning',
						message: '尚未通过验证',
						type: 'warning',
					});
					return next({ path: '/' });
				}
			}
		},
		component: () => import('../views/manage.vue'),
	},
];

const router = createRouter({
	history: createWebHistory('/'),
	routes,
});

export default router;
